/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * show_game.cc
 * Copyright (C) jurjen 2007 <jurjen@stwerff.xs4all.nl>
 *
 * show_game.cc is free software. You may redistribute it and/or modify it under
 * the terms of the GNU General Public License.
 */

#define DEBUG_ROUTINE "show_game"

#include <iostream>
#include "show_game.h"
#include "moros.h"
#include "../central/game.h"
#include "../general/log.h"
#include "game_window.h"

show_game::show_game(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml> refGlade)
: Gtk::Window(cobject), glade(refGlade) {
	show_game::present();
	glade->get_widget("show_name", name);
	glade->get_widget("show_description", description);
	glade->get_widget("show_join", join);
	glade->get_widget("show_goal", goal);
	glade->get_widget("show_map", map_name);
	glade->get_widget("game_owner", owner);
	glade->get_widget("show_parties", party_list);
	glade->get_widget("cancel_showgame", cancel_game);
	glade->get_widget("join_game", join_game);

	int colnr;
	Gtk::TreeViewColumn* col;
	Gtk::CellRendererText* renderer;

	desc_buffer=Gtk::TextBuffer::create();
	description->set_buffer(desc_buffer);

	party_store = Gtk::ListStore::create(cols);
	party_list->set_model(party_store);
	party_list->set_search_column(cols.name.index());

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = party_list->append_column("Name", *renderer);
	col = party_list->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.name);

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = party_list->append_column("Description", *renderer);
	col = party_list->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.description);

	cancel_game->signal_clicked().connect(sigc::mem_fun(*this, &show_game::clicked_cancel));
	join_game->signal_clicked().connect(sigc::mem_fun(*this, &show_game::clicked_join_game));
}

void show_game::load(long gamenr) {
	game_nr=gamenr;
	if (games.count(gamenr)>0) {
	    Gtk::TreeModel::const_iterator it;
		game g=games[gamenr];
		name->set_text(g.name);
		desc_buffer->set_text(g.description);
		if (g.join>=0 && g.join<join_elms) join->set_text(join_types[g.join]);
		if (g.goal>=0 && g.goal<goal_elms) goal->set_text(goal_types[g.goal]);
		map_name->set_text(g.map);
		owner->set_text(g.owner);
		vector<party>::iterator i;
  		for( i=g.parties.begin(); i != g.parties.end(); i++ ) {
			Gtk::TreeRow row = *(party_store->append());
			row[cols.name]=i->name;
			row[cols.description]=i->description;
		}
	}
}

show_game::~show_game() {}

void show_game::clicked_cancel() {
    dbg_calls("Clicked cancel");
	hide();
}

void show_game::clicked_join_game() {
    game_window *gw;
    glade->get_widget_derived("game", gw);
    gw->present();
}
